package com.wzq.permission.center.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wzq.permission.client.model.admin.DataPermissionModel;

/**  
 * 数据权限转换工具类  
 * @ClassName: DataPermissionConvertUtil    
 * @author 陈剑飞    
 * @date 2016年4月14日 下午8:26:12    
 * @version  v 1.0    
 */
public class DataPermissionConvertUtil {
    /**
     * dataPermissionModelList 转MapList，按范围分组
     * @author 陈剑飞    
     * @Title: convertToDataPermissionVO    
     * @param dataPermissionModelList
     * @return 
     * @Return: List<Map> 返回值 rangeType dataValueList
     */
    public static List<Map> convertToDataPermissionVO(List<DataPermissionModel> dataPermissionModelList){
        List<Map> dataPermissionMapList = null;
        if(dataPermissionModelList != null && dataPermissionModelList.size() > 0){
            dataPermissionMapList = new ArrayList<Map>();
            Map<String,List<String>> result = new HashMap<String,List<String>>();
            for(DataPermissionModel dataPermissionModel:dataPermissionModelList){
                 String rangeType = dataPermissionModel.getRangeType();
                 List<String> dataValueList = result.get(dataPermissionModel.getRangeType());
                 if(dataValueList == null){
                     List<String> initDataValue = new ArrayList<String>(); 
                     if("5".equals(rangeType)){
                         String[] dataValueStr = dataPermissionModel.getDataValue().split(",");
                         for(String dataValue:dataValueStr){
                             initDataValue.add(dataValue);
                         }
                     }else{
                         initDataValue.add(dataPermissionModel.getDataValue());
                     }
                     result.put(dataPermissionModel.getRangeType(), initDataValue);
                 }else{
                     if("5".equals(rangeType)){
                         String[] dataValueStr = dataPermissionModel.getDataValue().split(",");
                         for(String dataValue:dataValueStr){
                             dataValueList.add(dataValue);
                         }
                     }else{
                         dataValueList.add(dataPermissionModel.getDataValue());
                     }
                 }
            }
            for(Map.Entry<String, List<String>> map:result.entrySet()){
                Map permissionMap = new HashMap();
                permissionMap.put("rangeType", map.getKey());
                permissionMap.put("dataValueList", map.getValue());
                dataPermissionMapList.add(permissionMap);
            }
        }
        return dataPermissionMapList;
    }
}
